package weeny.stackedGraph.pre;

import prefuse.util.ColorLib;
import stacked.graph.ColorPicker;
import stacked.graph.Layer;
import weeny.pre.util.IntColor;

public class LinearColorPicker implements ColorPicker{
	private int maxColor = IntColor.GREEN;
	private int minColor = IntColor.BLUE;
	public LinearColorPicker(){
		
	}
	public LinearColorPicker(int maxColor, int minColor) {
		this.maxColor = maxColor;
		this.minColor = minColor;
	}
	public void colorize(Layer[] layers) {
	    // find the largest layer to use as a normalizer
	    float maxSum = 0;
	    float maxVol = 0;
	    for (int i=0; i<layers.length; i++) {
	      maxSum = (float) Math.max(maxSum, layers[i].sum);
	      maxVol = (float) Math.max(maxVol, layers[i].volatility);
	    }
	 
	    // find the color for each layer
	    for (int i = 0; i < layers.length; i++) {
	      float normalizedVol = (float) Math.sqrt(layers[i].volatility/maxVol);
	      float normalizedSum = layers[i].sum / maxSum;
	      float shapedSum = (float)(1.0 - Math.sqrt(normalizedSum))*0.9f+0.1f;
	      int color = ColorLib.interp(maxColor, minColor, normalizedVol);
	      layers[i].rgb = ColorLib.interp(color, IntColor.WHITE, shapedSum);
	    }
	}
	public String getName() {
		return "Linear Picker";
	}
}
